<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>高级搜索</title>
		<link rel="stylesheet" href="../../utils/layui/css/layui.css">
	</head>
	<body>
		<div class="page_container"  id="view">
			
			<form class="layui-form" action=""  method="post"  onsubmit="return false;">
				    <table class="layui-table">
				      <tbody id="tbody">
						
				      </tbody>
				    </table>
					  <div class="layui-form-item">
					    <div class="layui-input-block">
					      <button class="layui-btn" lay-submit lay-filter="formDemo">搜索</button>
					      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
					    </div>
					  </div>
			</form>
		</div>	
		
		<script src="/admin/utils/layui/layui.js" charset="utf-8"></script>
		<script src="/admin/utils/layui/config.js" charset="utf-8"></script>
		<script>
			var optionNumber = '<option value="=">等于</option><option value="IS NULL">是空</option><option value="IS NOT NULL">非空</option><option value="LIKE">模糊</option><option value=">">大于</option><option value=">=">大于等于</option><option value="<">小于</option><option value="<=">小于等于</option><option value="!=">不等于</option><option value="IN">IN(...)</option><option value="NOT IN">NOT IN</option><option value="BETWEEN">介于</option><option value="NOT BETWEEN">不介于</option>';
							  
			var optionStr = '<option value="=">等于</option><option value="IS NULL">是空</option><option value="IS NOT NULL">非空</option><option value="LIKE">模糊</option>';		  
			var optionEqual  = '<option value="=">等于</option>';	
			var optionIn  = '<option value="IN">IN(...)</option>';	
		    var optionBetween = '<option value="BETWEEN">介于</option><option value="NOT BETWEEN">不介于</option>';
			layui.use(['form', 'helper', 'laydate'], function(){
				var $ = layui.$;
				var laydate = layui.laydate;
				var data = eval('('+$("#menu-data", parent.document).val()+')');
				var dfCookie = {};
				try{
					var socookie = getcookie(parent.$_GET['api']+'_so');
					socookie = eval('('+socookie+')');
					for(i in socookie){
						var cookie = socookie[i];
						if(cookie[2] != '1'){
							rv = window.atob(cookie[2]);
							rv = escape(rv);
							rv = decodeURI(decodeURIComponent(rv));
							cookie[2] = rv;
						}else{
							cookie[2] = '';
						}
						dfCookie[cookie[0]] = cookie;
					}
				}catch(e){
					console.error(e.message);
				}
				window.menuData = data;
				var html = '';
				
				for(i in data.table_struct.field){
					var dfvalue = '';
					if(isset(dfCookie[i])){
						dfvalue = dfCookie[i][2];
					}
					var item = data.table_struct.field[i];
					var itemHtml = '<input type="text" name="value['+i+']" id="'+i+'" autocomplete="off" class="layui-input '+item['input_type']+'" value="'+dfvalue+'">';
					var optionHtml = optionStr;
					switch (item['input_type']){
						case 'radio':
							optionHtml = optionEqual;
							itemHtml = '';
							itemHtml += '<select name="value['+i+']">';
							itemHtml += '<option value="">请选择</option>';
							for(ii in item.value){
								var selected = '';
								if(ii == dfvalue){
									selected = "selected";
								}
								itemHtml += '<option value="'+ii+'" '+selected+'>'+item.value[ii]+'</option>';
							}
							itemHtml += '</select>';
							break;
						case 'checkbox':
							optionHtml = optionEqual;
							itemHtml = '';
							itemHtml += '<select name="value['+i+']">';
							itemHtml += '<option value="">请选择</option>';
							for(ii in item.value){
								var selected = '';
								if(ii == dfvalue){
									selected = "selected";
								}
								itemHtml += '<option value="'+ii+'" '+selected+'>'+item.value[ii]+'</option>';
							}
							itemHtml += '</select>';
							break;
						case 'select':
							optionHtml = optionEqual;
							itemHtml = '';
							itemHtml += '<select name="value['+i+']">';
							itemHtml += '<option value="">请选择</option>';
							for(ii in item.value){
								var selected = '';
								
								var itval = item.value[ii];
								if(itval.hasOwnProperty('key')){
									if(itval.key == dfvalue){
										selected = "selected";
									}
									itemHtml += '<option value="'+itval.key+'" '+selected+'>'+itval.val+'</option>';
								}else{
									if(ii == dfvalue){
										selected = "selected";
									}
									itemHtml += '<option value="'+ii+'" '+selected+'>'+item.value[ii]+'</option>';
								}
								
							}
							itemHtml += '</select>';
							break;	
						case 'date':
							optionHtml = optionBetween;
							break;	
						case 'time':
							optionHtml = optionBetween;
							break;		
						default:
							//如果value值有值的话
							if(item['value'].length > 0){
								optionHtml = optionEqual;
								itemHtml = '';
								itemHtml += '<select name="value['+i+']">';
								itemHtml += '<option value="">请选择</option>';
								for(ii in item.value){
									var selected = '';
									if(ii == dfvalue){
										selected = "selected";
									}
									itemHtml += '<option value="'+ii+'" '+selected+'>'+item.value[ii]+'</option>';
								}
								itemHtml += '</select>';
							}else if(item['type'] == 'int'){
								optionHtml = optionNumber;
							}
							break;
					}
					if(item.search === true){
						html += '<tr>\
						  <td width="120px" align="right">'+item['label']+'</td>\
						  <td width="60px" align="right">\
							  <select lay-ignore style="height: 38px; width:80px" name="condition['+i+']" class="condition" data-field="'+i+'">'+optionHtml+'</select>\
						  </td>\
						  <td>'+itemHtml+'</td>\
						</tr>';
					}
				}
				$("#tbody").html(html);
				var form = layui.form;
				
				
				$(".date").each(function(index){
					laydate.render({
					  elem: '#' + this.id, //指定元素
					  range: ','
					});
				});				
								
				//时间输入框
				$(".time").each(function(index){
					laydate.render({
					  elem: '#' + this.id, //指定元素
					  type: 'datetime',
					  range: ','
					});
				});
				
				form.render();
				
				$(".condition").each(function(index){
					var field = $(this).data('field');
					$(this).find('option').each(function(index){
						try{
							if(dfCookie[field]){
								dfSelect = dfCookie[field][1];
								if($(this).val() == dfSelect){
									$(this).attr("selected", true);
								}
							}
						}catch(e){
							console.error(e.message);
							//TODO handle the exception
						}
					});
				});
				
				//开始处理搜索
				form.on('submit(formDemo)', function(data){
					//var json = [[$("#linesokey").val(),'like',window.btoa(encodeURI('%'+$("#linesoval").val()+'%'))]];
					var formData = [];
					for(i in data.field){
						if(strrpos(i, 'value[') >= 0){
							var key = i.replace("value[", "condition[");
							var evalStr = 'var condition = data.field["'+key+'"];';								
							eval(evalStr);
							if(data.field[i] != '' || in_array(condition, ['IS NULL', 'IS NOT NULL'])){
								var name = i.replace("value[", "");
								name = name.replace("]", "");
								name = explode('[' ,name)[0];
								var value = '';
								//拿到这个值的类型
								data.field[i] = data.field[i].replace(new RegExp(" ", 'g'), "");
								if(in_array(menuData.table_struct.field[name]['input_type'], ['date', 'time']) && menuData.table_struct.field[name]['type'] == 'int'){
									var arr = explode(',', data.field[i]);
									var time0 = strtotime(arr[0]);
									var time1 = strtotime(arr[1]);
									data.field[i] = time0+','+time1;
								}
								if(!in_array(condition, ['IS NULL', 'IS NOT NULL'])){
									value = window.btoa(encodeURI(data.field[i]));
								}else{
									value = 1;
								}
								array_push(formData, [
									name,
									condition,
									value
								]);
							}
						}
					}
					if(formData != false){
						setcookie(parent.$_GET['api']+'_so', JSON.stringify(formData), 3600*24);
						parent.window.location.reload();
					}else{
						setcookie(parent.$_GET['api']+'_so', "", 0);
						parent.window.location.reload();
					}
					
				});
			});
			
		</script>
	</body>
</html>
